<template>
  <div class="app-container table-app-container">
    <div class="fixed-table">
      <div class="fixed-table-actionbar">
        <TableFilter />
        <ActionBar v-role="['workerGroupLeader']" />
      </div>
      <PageableTable
        :loading="loading"
        :data="insideTableData"
        :total="totalElements"
        :page="currentPage"
        :page-size="pageSize"
        :table-height="tableHeight"
        class="fixed-table-table"
        @page-change="onPageChange"
        @sort-change="onSortChange"
      >
        <el-table-column v-for="column in insideColumns" :key="column.key" :label="column.title" :width="column.width" :prop="column.key" :sortable="column.sortable">
          <template slot-scope="scope">
            <!-- <span v-html="brightenKeyword(scope.row.strContenet,filters.strContent)" /> -->
            <column-render :render="column.render" :row="scope.row" :column="column" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button v-if="isMine(row.userCode)" v-role="['workerGroupLeader']" title="编辑" type="text" size="mini" icon="el-icon-edit" @click="edit(row.id)" />
            <el-button v-if="isMine(row.userCode)" v-role="['workerGroupLeader']" title="删除" type="text" size="mini" icon="el-icon-delete" @click="remove(row.id)" />
            <el-button type="text" title="查看" size="mini" icon="el-icon-notebook-2" @click="view(row.id)" />
          </template>
        </el-table-column>
      </PageableTable>
    </div>
    <ViewDialog title="查看" class="fixed-dialog-height" :show.sync="dialogViewVisible" width="60%" @on-cancel="viewCancel">
      <ViewForm v-if="dialogViewVisible" :id="editId" />
    </ViewDialog>
    <FormDialog title="编辑" :show.sync="dialogFormVisible" width="40%" @on-ok="ok" @on-cancel="cancel">
      <EditForm v-if="dialogFormVisible" :id="editId" ref="editForm" />
    </FormDialog>
  </div>
</template>

<script>
// secondary package based on el-pagination
import ViewDialog from '@neu/biz-core/components/ViewDialog'
import FormDialog from '@neu/biz-core/components/FormDialog'
import TableFilter from './components/tableFilter'
import EditForm from './components/editForm'
import ViewForm from './components/viewForm'
import ActionBar from './components/actionBar'
import PageableTable from '@neu/biz-core/components/PageableTable'
import databind from '@neu/biz-core/mixins/databind'
import { formatDate } from '@neu/biz-core/utils/utils'
import { page, remove } from '@neu/dcom-ui/api/hnInspectionItem'
import { findByDictionaryType } from '@neu/dcom-ui/api/dictionary'
import { mapGetters } from 'vuex'
export default {
  name: 'InspectionItem',
  components: {
    TableFilter,
    ViewDialog,
    FormDialog,
    EditForm,
    ViewForm,
    ActionBar,
    PageableTable
    // SelectDialog
  },
  // 加入混入，使表格混入更多的api操作
  mixins: [databind],
  props: {

  },
  data() {
    return {
      r: page,
      d: remove,
      // 巡检类型
      types: [],
      // 数据类型
      dataTypes: [{ name: '单选', code: 'select' }, { name: '整数', code: 'number' }],
      insideColumns: [
        { title: '巡检分类', key: 'category', sortable: 'custom', display: true, render: (h, params) => {
          const category = params.row.category
          const des = category === 'day' ? '日巡检' : category === 'regular' ? '定期健康巡检' : '重保服务'
          return h('span', des)
        } },
        { title: '类别', key: 'itemType', sortable: 'custom', display: true, render: (h, params) => {
          const status = this.types.filter(t => {
            return t.code === params.row.itemType
          })
          const statusDesc = status.length === 1 ? status[0].text : params.row.statusDesc
          return h('span', statusDesc)
        } },
        { title: '内容', key: 'item', sortable: 'custom', display: true },
        { title: '数据类型', key: 'dataType', display: true, render: (h, params) => {
          const status = this.dataTypes.filter(t => {
            return t.code === params.row.dataType
          })
          const statusDesc = status.length === 1 ? status[0].name : params.row.statusDesc
          return h('span', statusDesc)
        }
        },
        { title: '单位', key: 'unit', sortable: 'custom', display: true },
        { title: '异常判断规则', key: 'rule', display: true },
        { title: '状态', key: 'disabled', sortable: 'custom', display: true, render: (h, params) => {
          if (!params.row.disabled) {
            return h('span', [
              h('el-tag', '启用')
            ])
          } else {
            return h('span', [
              h('el-tag', {
                props: {
                  type: 'info'
                }
              }, '禁用')
            ])
          }
        } },
        { title: '维护人', key: 'userName', sortable: 'custom', display: true },
        { title: '维护时间', key: 'updateTime', sortable: 'custom', render: (h, params) => { return h('span', formatDate(params.row.updateTime)) }, display: true }
      ],
      dialogFormVisible: false,
      dialogViewVisible: false,
      dialogStatus: 'create',
      editId: -1
    }
  },
  computed: {
    ...mapGetters([
      'username'
    ])
  },
  created() {
    findByDictionaryType('hnInspectionItemType').then(res => {
      if (res.status === 'success') {
        this.types = res.data
      }
    })
    this.filter()
  },
  methods: {
    view(id) {
      // 打开对话框
      this.editId = id
      this.dialogViewVisible = true
    },
    // 表格编辑按钮点击事件处理
    edit(id) {
      // 打开对话框
      this.editId = id
      this.dialogFormVisible = true
      // 更新当前被编辑实体的ID，自动传递至编辑表单
    },
    // 编辑资产对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
      })
    },
    cancel() {
      this.editId = -1
      this.$refs.editForm.reset()
      // 关闭对话框
      this.dialogFormVisible = false
    },
    viewCancel() {
      this.editId = -1
      // 关闭对话框
      this.dialogViewVisible = false
    },
    isMine(userCode) {
      return userCode === this.username
    }
  }
}
</script>
